'''
 * CaRComp Execution Environment
 * 
 * Copyright (C) 2011 Taylor L. Riche
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
'''

from carcomp.framework import io_component

class FileReaderComponent(io_component.IOComponent):
    '''
    classdocs
    '''

    def __init__(self, name=None, comp_name=None):
        '''
        Constructor
        '''
        # TLR 2011/03/06: Fixed bug where all readers had same name. Now use (hopefully) unique filenames
        if name == None:
            name = 'INSTREAM.txt'
        if comp_name == None:
            comp_name = name
        io_component.IOComponent.__init__(self, comp_name)
        self._core_file = open(name, 'r')
        
    def cleanup(self):
        self._core_file.close()
        
    def handle_input_stream(self, msg):
        for line in self._core_file:
            #print(self._name + ' ' + line)
            self.send_out('out', line)
        self.send_kill_msg()
        #print('Finished reading from file')
